package com.hainabo.mgcmall.api;

import com.hainabo.bootside.core.common.ReturnVO;
import com.hainabo.mgcmall.model.Infomation;
import com.hainabo.mgcmall.dao.InfomationRepository;
import com.hainabo.mgcmall.util.DataTablePageUtil;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.*;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import java.util.List;

/**
 * Created by 赵光辉 on 2017-11-14
 */

@Api(value = "/infomation")
@Component
@Path("v1/infomation")
@Produces(MediaType.APPLICATION_JSON)
@Slf4j
public class InfomationResource {
    /**
     * 查找所有的发布活动
     */
    @Autowired
    InfomationRepository InfomationRepository;
    @Autowired
    JdbcTemplate jdbcTemplate;

    @GET
    @Path("/list")
    public DataTablePageUtil list(@Context HttpServletRequest request) {
        DataTablePageUtil dataTablePageUtil = new DataTablePageUtil(request);
        String start = request.getParameter("start");
        String length = request.getParameter("length");
        StringBuilder sb = new StringBuilder();
        sb.append(
                "SELECT\n" +
                        "i.info_id,\n" +
                        "i.address,\n" +
                        "i.ch_status,\n" +
                        "i.en_id,\n" +
                        "i.info_status,\n" +
                        "i.p_name,\n" +
                        "i.publish_time,\n" +
                        "i.title,\n" +
                        "i.total_person,\n" +
                        "i.total_person,\n" +
                        "i.sh_time\n" +
                        "FROM\n" +
                        "kjt_infomation AS i\n" +
                        "WHERE\n" +
                        "1 = 1");
        sb.append(" ORDER BY i.info_id");
//        if(){
//
//        }else if(){
//
//        }else if(){
//
//        }else if(){
//
//        }
        String countSql = "select count(*) totalNum from (" + sb.toString() + ") i";
        List rows = jdbcTemplate.queryForList(sb.toString());
        dataTablePageUtil.setData(rows);
        int count = Integer.parseInt(jdbcTemplate.queryForList(countSql).get(0).get("totalNum").toString());
        dataTablePageUtil.setRecordsTotal(count);
        dataTablePageUtil.setRecordsFiltered(count);
        return dataTablePageUtil;
    }

    /**
     * 进行站内发布活动的添加
     *
     * @param infomation
     * @return
     */
    @POST
    @Path("/publish")
    public ReturnVO add(Infomation infomation) {
        return new ReturnVO(InfomationRepository.save(infomation));
    }

    /**
     * 进行审核,返回新闻信息在列表进行展示
     * 审核通过退回的功能
     *
     * @param id
     * @return
     */
    @PUT
    @Path("/edit/{id}")
    public ReturnVO edit(@PathParam("id") Long id, @RequestParam String param) {
        log.debug("");
        Infomation infomation = InfomationRepository.findOne(id);
        if (param.equals("yes")) {
            infomation.setChStatus("1");
            return new ReturnVO();
        } else if (param.equals("no")) {
            infomation.setChStatus("2");
            return new ReturnVO();
        } else {
            return new ReturnVO(infomation);
        }
    }
}
